package com.adb.androidwebalbum.uc;

import java.util.ArrayList;

import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import com.adb.androidwebalbum.R;
import com.adb.androidwebalbum.bean.ImageBean;
import com.adb.androidwebalbum.utils.Log;

public class CustomBottomBar implements OnClickListener{
	
	private String TAG = CustomBottomBar.class.getCanonicalName();
	private Context mContext = null;
	private LayoutInflater mLayoutInflater = null;
	private PopupWindow mPopupWindow = null;
	private boolean mIsShowing = false;
	public Button btn_Previous, btn_Next;
	
	
	OnHeadNavigationSelectedListener mCallBack;
	
	
	public boolean isShowing() { return mIsShowing; }
	
	public CustomBottomBar(Context context, LayoutInflater lo) {
		mContext = context;
		mLayoutInflater = lo;
		mCallBack = (OnHeadNavigationSelectedListener)context;
	}
	
	
	@SuppressWarnings("deprecation")
	public synchronized void show(View v,int mPosition,ArrayList<ImageBean> arrListImageBean) {
		
		Log.debug(TAG,"[ CustomBottomBar ] ");
		mIsShowing = true;
		if (mPopupWindow != null) return; //already showing
		Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		View mView= mLayoutInflater.inflate(R.layout.custom_bottom_menu, null);
		
		btn_Previous = (Button)mView.findViewById(R.id.btn_prv);
		btn_Next = (Button)mView.findViewById(R.id.btn_next);
		btn_Next.setOnClickListener(this);
		btn_Previous.setOnClickListener(this);
		
		// Take decision for button visibility
		setNavigationControl(mPosition, arrListImageBean);
		
		mPopupWindow = new PopupWindow(mView,android.view.ViewGroup.LayoutParams.MATCH_PARENT, 65, false);
        mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
    	mPopupWindow.setWidth(display.getWidth());
        mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
	}
	
	public synchronized void hide() {
		mIsShowing = false;
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
			mPopupWindow = null;
		}
		return;
	}

	public void onClick(View v) {
		mCallBack.onButtonSelected(v.getId());
	}
	
	public void setNavigationControl(int mPosition,ArrayList<ImageBean> arrListImageBean){
		
		if(isShowing()){
			
			if (mPosition == 0) {
				btn_Previous.setVisibility(View.GONE);
				btn_Previous.setBackgroundResource(R.drawable.btn_prv_disabled);
				btn_Previous.setEnabled(true);
				btn_Next.setBackgroundResource(R.drawable.btn_next_selector);
				btn_Next.setVisibility(View.VISIBLE);

			} else if (mPosition > 0 && !(mPosition == arrListImageBean.size() - 1)) {
				btn_Next.setBackgroundResource(R.drawable.btn_next_selector);
				btn_Next.setVisibility(View.VISIBLE);
				btn_Previous.setBackgroundResource(R.drawable.btn_prv_selector);
				btn_Previous.setVisibility(View.VISIBLE);

			} else if (mPosition > 0 && mPosition == arrListImageBean.size() - 1) {
				btn_Next.setVisibility(View.GONE);
				btn_Next.setBackgroundResource(R.drawable.btn_next_disabled);
				btn_Next.setEnabled(true);
				btn_Previous.setBackgroundResource(R.drawable.btn_prv_selector);
				btn_Previous.setVisibility(View.VISIBLE);
			}
		}
			

		
	}

}
